//package com.liu.cloud.config;
//
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.loadbalancer.reactive.Request;
//import org.springframework.cloud.client.loadbalancer.reactive.Response;
//import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
//import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
//import org.springframework.http.HttpHeaders;
//import reactor.core.publisher.Mono;
//
//import java.util.List;
//
//public class CustomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
//
//
//    @Override
//    public Mono<Response<ServiceInstance>> choose(Request request) {
//        ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
//                .getIfAvailable(org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier::new);
//        return supplier.get(request).next()
//                .map(serviceInstances -> processInstanceResponse(supplier, serviceInstances, request));
//    }
//
//
//    private Response<ServiceInstance> processInstanceResponse(ServiceInstanceListSupplier supplier,
//                                                              List<ServiceInstance> serviceInstances, Request request) {
//        HttpHeaders context = (HttpHeaders) request.getContext();
//
//
//        Map<Boolean, List<ServiceInstance>> collect = serviceInstances.stream()
//                .collect(Collectors.groupingBy(e -> e.getMetadata().containsKey(loadBalanceConfig.headTag)));
//        List<ServiceInstance> nextInstances = serviceInstances;
//        String version = context.getFirst(loadBalanceConfig.headTag);
//        if (version != null) {
//            nextInstances = collect.get(grayV); //灰度标记实列
//        } else {
//            nextInstances = collect.get(normal);
//        }
//
//        if (nextInstances.isEmpty()) { // 没能获取到灰度版本的实列,全实例兜底
//            nextInstances = serviceInstances;
//        }
//
//        org.springframework.cloud.client.loadbalancer.Response<ServiceInstance> serviceInstanceResponse = getInstanceResponse(nextInstances);
//        if (supplier instanceof SelectedInstanceCallback && serviceInstanceResponse.hasServer()) {
//            ((SelectedInstanceCallback) supplier).selectedServiceInstance(serviceInstanceResponse.getServer());
//        }
//        return serviceInstanceResponse;
//    }
//}